package com.wef.system.service.impl;

import java.util.Iterator;
import java.util.Set;

import com.gfa4j.exception.ValidateRuntimeException;
import com.gfa4j.hibernate.BaseObject;
import com.gfa4j.service.impl.BaseServiceImpl;
import com.wef.system.entity.MenuItemTitle;
import com.wef.system.entity.domain.MenuItemTitleDomain;
import com.wef.system.service.MenuItemTitleService;

/**
 * @FileName MenuItemTitleServiceImpl.java
 * @Summary  功能描述：国际化的子表信息。
 * @Author   LJ
 * @Version  1.0
 * @Date     2010/02/07
 * @example  使用示例：略。
 */

public class MenuItemTitleServiceImpl extends BaseServiceImpl implements
		MenuItemTitleService {

	public MenuItemTitle addMenuItemTitle(MenuItemTitle menuItemTitle) {
		return (MenuItemTitle)super.saveObject(menuItemTitle);
	}

	public MenuItemTitle loadMenuItemTitle(String id) {
		return (MenuItemTitle)super.load(MenuItemTitle.class, id);
	}

	public MenuItemTitle updateMenuItemTitle(MenuItemTitle menuItemTitle) {
		return (MenuItemTitle)super.updateObject(menuItemTitle);
	}

	public void deleteMenuItemTitle(MenuItemTitle menuItemTitle) {
		super.deleteObject(menuItemTitle);
	}
	
	public void deleteMenuItemTitles(Set<MenuItemTitle> menuItemTitles) {
		for(MenuItemTitle menuItemTitle: menuItemTitles){
			deleteMenuItemTitle(menuItemTitle);
		}
	}
	
	public MenuItemTitle saveOrUpdateMenuItemTitle(MenuItemTitle menuItemTitle) {
		if (menuItemTitle.getRowStatus() == BaseObject.ROWSTATUS_ADDED) {
			return this.addMenuItemTitle(menuItemTitle);
		}
		if (menuItemTitle.getRowStatus() == BaseObject.ROWSTATUS_MODIFIED) {
			return this.updateMenuItemTitle(menuItemTitle);
		}
		if (menuItemTitle.getRowStatus() == BaseObject.ROWSTATUS_DELETED) {
			this.deleteMenuItemTitle(menuItemTitle);
			return new MenuItemTitle();
		}
		return menuItemTitle;
	}

	public void saveOrUpdateMenuItemTitles(Set<MenuItemTitle> menuItemTitles, String menuItemId) {
		StringBuilder errorString = new StringBuilder();
		try {
			deleteObjectByProperty(MenuItemTitle.class, MenuItemTitleDomain.menuItemId, menuItemId);
		} catch (ValidateRuntimeException ex) {
			errorString.append(ex.getMessage() + "\n");
		}
		
		for (Iterator<MenuItemTitle> it = menuItemTitles.iterator(); it.hasNext();) {
			MenuItemTitle menuItemTitle = (MenuItemTitle) it.next();
			menuItemTitle.setMenuItemId(menuItemId);
			this.addMenuItemTitle(menuItemTitle);
		}	
		if (errorString.length() > 0) {
			throw new ValidateRuntimeException(errorString.toString());
		}
	}
}
